package br.com.hsp.system.beans;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import br.com.hsp.organizacao.Instituicao;
import br.com.hsp.system.repository.GerenteInstituicaoRepository;
import br.com.hsp.system.repository.InstituicaoRepository;
import br.com.hsp.users.GerenteInstituicao;

@ManagedBean
public class GerenteInstituicaoBean {

	private GerenteInstituicao gerenteInstituicao = new GerenteInstituicao();	
	
	private Long instituicaoID;
		
	private List<GerenteInstituicao> gerentesInstituicao; 

	public void add() {
		EntityManager manager = this.getManager();
		InstituicaoRepository instituicaoRepository = new InstituicaoRepository(manager) ;
		GerenteInstituicaoRepository gerenteInstituicaoRepository = new GerenteInstituicaoRepository(manager);

		if(this.instituicaoID !=  null) {
			Instituicao instituicao = instituicaoRepository.procura(this.instituicaoID);
			this.gerenteInstituicao.setInstituicao(instituicao);
		}
		
		if (this.gerenteInstituicao.getId() == null) {
			gerenteInstituicaoRepository.add(this.gerenteInstituicao);
		} else {
			gerenteInstituicaoRepository.atualiza(this.gerenteInstituicao);
		}
		this.gerenteInstituicao = new GerenteInstituicao();
		this.instituicaoID = null;
	}

	public void preparaAlteracao() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		GerenteInstituicaoRepository repository = new GerenteInstituicaoRepository(manager);
		this.gerenteInstituicao = repository.procura(id);
		this.instituicaoID = this.gerenteInstituicao.getInstituicao().getId();

	}
	

	public void remove() {
		System.out.println("remove");
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		GerenteInstituicaoRepository repository = new GerenteInstituicaoRepository(manager);
		repository.remove(id);
		this.gerenteInstituicao = null;
		this.gerentesInstituicao = null;
	}
	

	private EntityManager getManager() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		return (EntityManager) request.getAttribute("EntityManager");

	}

	public List<GerenteInstituicao> getGerentesInstituicao() {
		if (this.gerentesInstituicao == null) {
			EntityManager manager = this.getManager();
			GerenteInstituicaoRepository repository = new GerenteInstituicaoRepository(manager);
			this.gerentesInstituicao = repository.getLista();
		}
		return this.gerentesInstituicao;
	}

	public GerenteInstituicao getGerenteInstituicao() {
		return gerenteInstituicao;
	}

	public void setGerenteInstituicao(GerenteInstituicao gerenteInstituicao) {
		this.gerenteInstituicao = gerenteInstituicao;
	}

	public Long getInstituicaoID() {
		return instituicaoID;
	}

	public void setInstituicaoID(Long instituicaoID) {
		this.instituicaoID = instituicaoID;
	}

	
	

}
